﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OleDb;

public partial class UserConTrols_Sach : System.Web.UI.UserControl
{
    DataProvider duLieu = new DataProvider();

    protected void Page_Load(object sender, System.EventArgs e)
    {
        if (!IsPostBack)
        {
            //==== Phần Trang ====
            int sohangtren1trang = (int)Application["SoSPTren1TrangKhiPhanTrang"];

            int vitri1 = 1;
            int vitri2 = sohangtren1trang;

            int total = duLieu.LayTongHangHoa("hanghoa");
            DataTable bang1 = null;

            if (total > sohangtren1trang)
            {
                Panelphantrang1.Visible = true;
                Panelphantrang2.Visible = true;
            }
            else
            {
                Panelphantrang1.Visible = false;
                Panelphantrang2.Visible = false;
            }

            string index = Request.QueryString["index"];
            string page = Request.QueryString["page"];

            if (!string.IsNullOrEmpty(index) & page == "next")
            {
                if (int.Parse(index) < sohangtren1trang)
                    index = "1";

                vitri1 =int.Parse(index);
                vitri2 = (int.Parse(index) < sohangtren1trang ? vitri1 + (sohangtren1trang - 1) : int.Parse(index) + sohangtren1trang);

                if (vitri2 > total)
                {
                    vitri2 = total;
                    vitri1 = total - sohangtren1trang;
                }


            }
            else if (!string.IsNullOrEmpty(index) & page == "back")
            {
                if (int.Parse(index) > sohangtren1trang)
                {
                    vitri1 = int.Parse(index) - sohangtren1trang;
                }
                else
                {
                    vitri1 = 0;
                }
                vitri2 = vitri1 + sohangtren1trang;
            }

            bang1 = duLieu.phanTrang(vitri1, vitri2, "select NHOMHANGHOA.*, LOAIHANGHOA.TENLOAIHH, HANGHOA.*, NHASX.TENNHASX FROM NHASX INNER JOIN HANGHOA ON NHASX.MANHASX = HANGHOA.MANHASX INNER JOIN LOAIHANGHOA ON HANGHOA.MALOAIHH = LOAIHANGHOA.MALOAIHH INNER JOIN NHOMHANGHOA ON LOAIHANGHOA.MANHOMHH = NHOMHANGHOA.MANHOMHH order by soluongton asc");

            trangdautien1.NavigateUrl = "../Default.aspx?page=back&index=1#pagging";
            trangdautien2.NavigateUrl = "../Default.aspx?page=back&index=1#pagging";

            trangsau1.NavigateUrl = "../Default.aspx?page=back&index=" + vitri1 + "#pagging";
            trangsau2.NavigateUrl = "../Default.aspx?page=back&index=" + vitri1 + "#pagging";

            trangtiep1.NavigateUrl = "../Default.aspx?page=next&index=" + vitri2 + "#pagging";
            trangtiep2.NavigateUrl = "../Default.aspx?page=next&index=" + vitri2 + "#pagging";

            trangcuoi1.NavigateUrl = "../Default.aspx?page=next&index=" + (total - 6) + "#pagging";
            trangcuoi2.NavigateUrl = "../Default.aspx?page=next&index=" + (total - 6) + "#pagging";

            tongconghanghoa1.Text = "<font color='blue'>Đang xem</font>&nbsp;<font color='red'>" + vitri1 + " - " + vitri2 + "</font>&nbsp;<font color='blue'>của&nbsp;</font><font color='red'>" + total + "</font>&nbsp;<font color='blue'>sản phẩm</font>";
            tongconghanghoa2.Text = "<font color='blue'>Đang xem</font>&nbsp;<font color='red'>" + vitri1 + " - " + vitri2 + "</font>&nbsp;<font color='blue'>của&nbsp;</font><font color='red'>" + total + "</font>&nbsp;<font color='blue'>sản phẩm</font>";

            //=== Load Dữ Liệu Vào DATALIST ====

            if (bang1.Rows.Count != 0)
            {
                DataList1.DataSource = bang1;
                DataList1.DataBind();
            }
            else
            {
                showthongbaosachmoi.Text = duLieu.BangThongBao("THÔNG BÁO", "<a href='Default.aspx'>HIỆN TẠI SIÊU THỊ HẾT HÀNG. MONG QUÝ KHÁCH THÔNG CẢM</a>");
            }

        }
    }

    public string Lay_DATE_Chuan_VN(string DuLieu)
    {
        return System.DateTime.Parse(DuLieu).Day + "/" + System.DateTime.Parse(DuLieu).Month + "/" + System.DateTime.Parse(DuLieu).Year;
    }

    public string rutgonten(string noidung, int chieudai, int kieu)
    {
        //=== Nếu Kiểu = 1 => Có DIV
        //=== Nếu Kiểu = 2 => Ko Có DIV
        if (kieu == 1)
        {
            return "<div title='" + noidung + "'>" + (noidung.Length > chieudai ? noidung.Substring(0, chieudai-1) + "..." : noidung) + "</div>";
        }
        else if (kieu == 2)
        {
            return (noidung.Length > chieudai ? noidung.Substring(0, chieudai-1) + "..." : noidung);
        }
        else
        {
            return "0";
        }
    }
}